package com.warm.base.algorithm.structure.array;


/**
 * @Date: 2019/12/2 10:52
 * @Author: LChuang
 * @Description: our own array
 */
public class Array<E> {

    private E[] data;

    private int size;

    /**
     * 传入数组容量 capacity 构造 Array
     * @param capacity
     */
    public Array(int capacity){
        data = (E[]) new Object[capacity];
        size = 0;
    }

    /**
     * 默认数组容量 capacity 10
     */
    public Array(){
        this(10);
    }

    /**
     * 数组容量
     * @return
     */
    public int getCapacity(){
        return data.length;
    }

    /**
     * 元素个数
     * @return
     */
    public int getSize(){
        return size;
    }

    public boolean isEmpty(){
        return size == 0;
    }

    /**
     * 在 index 位置插入新元素 e
     * @param index
     * @param e
     */
    public void add(int index, E e){
        if(index < 0 || index > size){
            throw new IllegalArgumentException("add failed. Require index > size");
        }

        if(size == data.length){
            // 扩容
            resize(2*data.length);
        }

        for(int i = size-1; i >= index; i--){
            data[i+1] = data[i];
        }
        data[index] = e;
        size++;
    }

    public void addLast(E e){
        add(size, e);
    }

    public void addFirst(E e){
        add(0, e);
    }

    /**
     * 获取 index 位置的值
     * @param index
     * @return
     */
    public E get(int index){
        if(index < 0 || index >= size){
            throw new IllegalArgumentException("get failed. Index is illegal");
        }
        return data[index];
    }

    public E getLast(){
        return get(size - 1);
    }

    public E getFirst(){
        return get(0);
    }

    /**
     * 修改 index 索引位置的元素为 e
     * @param index
     * @param e
     */
    public void set(int index, E e){
        if(index < 0 || index >= size){
            throw new IllegalArgumentException("set failed. Index is illegal");
        }
        data[index] = e;
    }

    /**
     * 是否包含 元素 e
     * @param e
     * @return
     */
    public boolean contains(E e){
        for (int i = 0; i < size; i++){
            if(data[i] == e){
                return true;
            }
        }
        return false;
    }

    /**
     * 查找元素 e 的 index
     * @param e
     * @return
     */
    public int find(E e){
        for (int i = 0; i < size; i++){
            if (data[i] == e){
                return i;
            }
        }
        return -1;
    }

    /**
     * 删除 index 位置元素, 并返回删除的元素
     * @param index
     * @return
     */
    public E remove(int index){
        if(index < 0 || index >= size){
            throw new IllegalArgumentException("remove failed. Index is illegal");
        }
        E ret = data[index];
        for (int i = index+1; i < size; i++){
            data[i-1] = data[i];
        }
        size--;
        // loitering(游荡的) objects != memory leak(泄漏)
        data[size] = null;
        if(size == data.length / 2){
            // 缩容
            resize(data.length/2);
        }
        return ret;
    }

    public E removeFirst(){
        return remove(0);
    }

    public E removeLast(){
        return remove(size - 1);
    }

    /**
     * 将数组空间的容量变成 newCapacity 大小
     * @param newCapacity
     */
    private void resize(int newCapacity){
        E[] newData = (E[]) new Object[newCapacity];
        for (int i = 0; i < size; i++){
            newData[i] = data[i];
        }
        data = newData;
    }

    public void removeElement(E e){
        int index = find(e);
        if(index != -1){
            remove(index);
        }
    }

    @Override
    public String toString(){

        StringBuilder res = new StringBuilder();
        res.append(String.format("Array: size = %d , capacity = %d\n", size, data.length));
        res.append('[');
        for(int i = 0 ; i < size ; i ++){
            res.append(data[i]);
            if(i != size - 1)
                res.append(", ");
        }
        res.append(']');
        return res.toString();
    }
}
